﻿package map {
	import common.KeyCode;
	import common.MainSprite;
	import common.Scene;
	import flash.display.*;
	import flash.net.*;
	import flash.text.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.events.*;
	import flash.utils.*;
	
	/**
	 * ワールドマップのシーン
	 */
	public class MapScene extends Scene {
		[Embed(source = '../../lib/title.jpg')]
		private static const BitmapTitle:Class;
		
		[Embed(source = '../../lib/start.png')]
		private static const BitmapStart:Class;
		
		[Embed(source = '../../lib/logout.png')]
		private static const BitmapLogout:Class;
		
		[Embed(source = '../../lib/next.png')]
		private static const BitmapNext:Class;
		
		[Embed(source = '../../lib/WorldMap01.jpg')]
		private static const BitmapWorldMap:Class;
		
		[Embed(source = '../../lib/Port-icon01.png')]
		private static const BitmapPortIcon:Class;
		
		private var loader:Loader;
 		
 		private var img:Sprite;
 		private var cursor:Sprite;
 		private var ncursor:int;
 		private var cursor2:Sprite;
 		private var ncursor2:int;
 		private var state:int;
 		
 		private var tformat:TextFormat;
 		
		private var PORTS:Array = [
			[ "Yokohama", 541, 205, "日本", "http://en.wikipedia.org/wiki/Port_of_Yokohama" ],
			[ "Hong Kong", 501, 233, "世界三大美港みっつめ候補", "http://en.wikipedia.org/wiki/Port_of_Hong_Kong", ],
			[ "Sydney", 562, 345, "世界三大美港", "http://simple.wikipedia.org/wiki/Sydney_Harbour", ],
			[ "San Francisco", 92, 211, "世界三大美港みっつめ候補", "http://en.wikipedia.org/wiki/Port_of_San_Francisco", ],
			[ "Rio de Janeiro", 226, 316, "世界三大美港みっつめ候補", "http://en.wikipedia.org/wiki/Rio_de_Janeiro", ],
			[ "Napoli", 326, 198, "世界三大美港", "http://en.wikipedia.org/wiki/S.S.C._Napoli", ],
			[ "Cape Town", 331, 340, "世界三大美港みっつめ候補", "http://en.wikipedia.org/wiki/Cape_Town" ],
		];
		
		private var sptitle:Sprite;
		private var spend:Sprite;
		private var twitter:Twitter = new Twitter();
		
		public function MapScene(main:MainSprite) {
			super(main);
			_sprite = new Sprite();
		}
		private function title():void {
			sptitle = new Sprite();
			sptitle.addChild(new BitmapTitle());
			var bmp:Sprite = new Sprite();
			bmp.addChild(new BitmapStart());
			bmp.x = 484;
			bmp.y = 420;
			bmp.buttonMode = true;
			bmp.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
				if (twitter.debugMode()) {
					worldmap();
				} else {
					twitter.request();
				}
			});
			sptitle.addChild(bmp);
			_sprite.addChild(sptitle);
		}
		private function worldmap():void {
			img = new Sprite();
			img.addChild(new BitmapWorldMap());
			img.x = 0;
			img.y = 0;
			_sprite.addChild(img);
			
			{
				for (var i:int = 0; i < PORTS.length; i++) {
					var p:Array = PORTS[i];
					
					var icon:Sprite = new Sprite();
					icon.addChild(new BitmapPortIcon());
					icon.x = p[1] - 15;
					icon.y = p[2] - 15;
					icon.width = 30;
					icon.height = 30;
					img.addChild(icon);
				}
			}
			
			ncursor = 0;
			ncursor2 = -1;
			state = 0;
			
			cursor = new Sprite();
			cursor.graphics.beginFill(0xAF0033);
			cursor.graphics.drawCircle(0, 0, 20);
			cursor.alpha = .5;
			img.addChild(cursor);
			
			cursor2 = new Sprite();
			cursor2.graphics.beginFill(0x00AF33);
			cursor2.graphics.drawRect(-10, -10, 20, 20);
			cursor2.alpha = .5;
			img.addChild(cursor2);
			
			tformat = new TextFormat();
			tformat.font = "_等幅";
			tformat.color = 0xFF0000;
//			tformat.size = 120 * Capabilities.screenDPI / 72;
			tformat.size = 120;
			
			label = new TextField();
			label.autoSize = TextFieldAutoSize.LEFT;
			label.setTextFormat(tformat);
			label.text = Capabilities.playerType;
			label.x = 80;
			label.y = 20;
//			img.addChild(label);
			
			/*
			img.addEventListener(Event.ENTER_FRAME, function(e:Event):void {
				var x2:int = e.localX;
				var y2:int = e.localY;
				for (var i:int = 0; i < PORTS.length; i++) {
					var p:Array = PORTS[i];
					
					var x:int = p[1];
					var y:int = p[2];
					
					var dx:int = x - x2;
					var dy:int = y - y2;
					var d:int = Math.sqrt(dx * dx + dy * dy);
					
					if (d < 20) {
						ncursor = i;
						break;
					}
				}
				label.text = "enter";
			});
			*/
			img.addEventListener(MouseEvent.MOUSE_MOVE, function(e:MouseEvent):void {
				mouseCursor(e.stageX, e.stageY, false);
			});
			img.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
				mouseCursor(e.stageX, e.stageY, true);
			});
			
			var bmp:Sprite = new Sprite();
			bmp.addChild(new BitmapLogout());
			bmp.x = 484;
			bmp.y = 420;
			bmp.buttonMode = true;
			bmp.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
				twitter.logout();
				title();
			});
			img.addChild(bmp);
			
			setPosCursor();
		}
		private function mouseCursor(x2:int, y2:int, click:Boolean):void {
			var s:String = " " + x2 + "," + y2;
			for (var i:int = 0; i < PORTS.length; i++) {
				var p:Array = PORTS[i];
				
				var x:int = p[1];
				var y:int = p[2];
				
				var dx:int = x - x2;
				var dy:int = y - y2;
				var d:int = Math.sqrt(dx * dx + dy * dy);
				
				s += "\n" + d;
				if (d < 20) {
					s += " HIT!!";
					ncursor = i;
					setPosCursor();
					if (click) {
						invokeMainScene(0, ncursor);
						break;
					}
				}
			}
//			label.text = s;
		}
		private var label:TextField;
		public function init(isFirst:Boolean, isGoaled:Boolean):void {
			if (isFirst) {
				if (twitter.debugMode()) {
					title();
				} else {
					if (twitter.isLogin()) {
						worldmap();
					} else if (twitter.isRequested()) {
						twitter.checkLogin(function(ok:Boolean):void {
							if (ok) {
								worldmap();
							} else {
								title();
							}
						});
					} else {
						title();
					}
				}
			} else {
				// isgoald
				spend = new Sprite();
				spend.addChild(new BitmapTitle());
				var bmp:Sprite = new Sprite();
				bmp.addChild(new BitmapNext());
				bmp.x = 484;
				bmp.y = 420;
				bmp.buttonMode = true;
				bmp.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
					_sprite.removeChild(spend);
					worldmap();
				});
				spend.addChild(bmp);
				
				var tformat:TextFormat = new TextFormat();
	//			tformat.font = "_等幅";
				tformat.color = 0x000000;
	//			tformat.size = 120 * Capabilities.screenDPI / 72;
				tformat.size = 24;

				var label:TextField = new TextField();
	//			label.autoSize = TextFieldAutoSize.LEFT;
//				var s:String = "I've reached Port of " + PORTS[ncursor][0] + " #wwpb #ggj2010";
				var s:String = "I've reached Port of " + PORTS[ncursor][0] + " #wwpb";
				label.text = s;
				if (twitter.isLogin())
					twitter.twit(s);
				
				label.setTextFormat(tformat);
				label.autoSize = TextFieldAutoSize.LEFT;
				label.x = 20;
				label.y = 30;
				spend.addChild(label);
				
				_sprite.addChild(spend);
				
				navigateToURL(new URLRequest(PORTS[ncursor][4]), "_blank");
			}
		}
		public function setPosCursor():void {
			var p:Array = PORTS[ncursor];
			cursor.x = p[1];
			cursor.y = p[2];
			if (ncursor2 == -1) {
				cursor2.x = -1000; // disableがあれば
			} else {
				p = PORTS[ncursor2];
				cursor2.x = p[1];
				cursor2.y = p[2];
			}
		}
		public function onLoadComplete(event:Event):void {
		}
		public function get sprite():Sprite {
			return _sprite;
		}
		override public function onKeyDown(keyCode:KeyCode):void {
			if (keyCode == KeyCode.OK) {
				// 元と行き先選ぶ
				/*
				if (ncursor2 != -1) {
					if (ncursor != ncursor2) {
						invokeMainScene(ncursor, ncursor2);
					}
				} else {
					if (ncursor2 == PORTS.length - 1)
						ncursor2 = 0;
					else
						ncursor2 = ncursor + 1;
					setPosCursor();
				}
				*/
				// 簡易的に一箇所のみ
				invokeMainScene(0, ncursor);
			} else if (keyCode == KeyCode.CANCEL) {
				if (ncursor2 != -1) {
					ncursor2 = -1;
				}
				/*
			} else if (keyCode == KeyCode.RIGHT) {
				img.x += 30;
			} else if (keyCode == KeyCode.LEFT) {
				img.x -= 30;
				*/
			} else if (keyCode == KeyCode.UP) {
				if (ncursor2 == -1) {
					if (ncursor == 0)
						ncursor = PORTS.length - 1;
					else
						ncursor--;
					setPosCursor();
				} else {
					if (ncursor2 == 0)
						ncursor2 = PORTS.length - 1;
					else
						ncursor2--;
					setPosCursor();
				}
			} else if (keyCode == KeyCode.DOWN) {
				if (ncursor2 == -1) {
					if (ncursor == PORTS.length - 1)
						ncursor = 0;
					else
						ncursor++;
					setPosCursor();
				} else {
					if (ncursor2 == PORTS.length - 1)
						ncursor2 = 0;
					else
						ncursor2++;
					setPosCursor();
				}
			}
		}
		override public function onKeyUp(keyCode:KeyCode):void {
		}
	}
}
